package com.taxi.driver.module.main.mine.message;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.qianxx.view.xrecyclerview.IXRecyclerViewListener;
import com.qianxx.view.xrecyclerview.XRecyclerView;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.event.MsgEvent;
import com.taxi.driver.module.main.mine.message.dagger.DaggerMessageComponent;
import com.taxi.driver.module.main.mine.message.dagger.MessageModule;
import com.taxi.driver.module.main.mine.message.details.MessageDetailsActivity;
import com.taxi.driver.module.main.mine.wallet.MyWalletActivity;
import com.taxi.driver.module.vo.MessageVO;
import com.taxi.driver.widget.dialog.TwoSelectorDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 消息中心
 */
public class MessageActivity extends BaseActivity implements MessageContract.View {

    @BindView(R.id.x_recycler_view)
    XRecyclerView mXRecyclerView;

    @Inject
    MessagePresenter mPresenter;

    private MessageAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);

        DaggerMessageComponent.builder()
                .appComponent(Application.getAppComponent())
                .messageModule(new MessageModule(this))
                .build()
                .inject(this);

        initView();
        initListener();
    }

    private void initView() {
        mAdapter = new MessageAdapter(this);
        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mXRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        mXRecyclerView.setXRecyclerViewListener(new IXRecyclerViewListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh();
            }

            @Override
            public void onLoadMore() {
                mPresenter.loadMore();
            }
        });

        mAdapter.setOnItemClickListener((position, view, item) -> {
            if (item.type == 1) {
                MessageDetailsActivity.start(this, item);
            } else if (item.type == 3) {
                MyWalletActivity.start(this, item.uuid, item.status);
            }
        });

        mXRecyclerView.onRefresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.tx_head_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_head_right:
                new TwoSelectorDialog(this, getResources().getString(R.string.confirm_clean_all), null)
                        .setConfirmClickListener(dialog -> {
                            dialog.dismiss();
                            mPresenter.deleteAll();
                        })
                        .setCancelClickListener(Dialog::dismiss)
                        .show();
                break;
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void setList(List<MessageVO> vos) {
        if (vos.size() < 0) {
            mXRecyclerView.setLoadHide();
            return;
        }
        if (vos.size() < 10) {
            mXRecyclerView.setLoadComplete(true);
        } else {
            mXRecyclerView.setLoadComplete(false);
        }
        mAdapter.setAll(vos);
        mXRecyclerView.refreshComplete();
    }

    @Override
    public void appendList(List<MessageVO> vos) {
        if (vos.size() < 10) {
            mXRecyclerView.setLoadComplete(true);
        } else {
            mXRecyclerView.setLoadComplete(false);
        }
        mAdapter.addAll(vos);
    }

    @Override
    public void deleteSuccess() {
        toast(getResources().getString(R.string.delete_success));
        mAdapter.clear();
        mXRecyclerView.setLoadHide();
    }

    @Override
    public void deleteFail(int errorCode, String errorMsg) {
        toast(errorMsg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MsgEvent event) {
        switch (event.type) {
            case MsgEvent.REFRESH_STATUS: // 刷新指定消息的状态
                String uuid = (String) event.obj1;
                KLog.i(uuid);
                // 设置消息为已读状态
                mAdapter.setRead(uuid);
                mAdapter.notifyDataSetChanged();
                break;
        }
    }
}
